import { defHttp } from '@/utils/http';
import { root, sys } from '../apiRoot';
import type { IQuery, IResData } from '../indexTypes';

import type {
  FormStatistics,
  IFillOutTheForm,
  IFormTemplate,
  ServiceTicketFormTemplateDetailResult,
  ServiceTicketFormAddData,
  ServiceTicketFormAddOrUpdateItmeData,
  ServiceTicketFormDetailResult,
  ServiceTicketFormPublishSetData,
  StatisticsDetailList,
  SysUserResult,
  AddServiceTicketFormDtoData,
  IFormFillRecordList,
  IFormFillRecordListQuery,
  ServiceTicketFormSnapshootDetailResult,
  ServiceFormExportDetail,
} from './types';
import type { RecordTypeEnum, ServiceFormStatusEnum } from '@/enums/serviceFormEnum';

const Api = {
  /** 管理员列表 */
  sysUserList: sys + 'user/pageList',
  serviceTicketFormDetail: sys + 'service-ticket-form/getById',
  serviceTicketFormAddOrUpdateItme: sys + 'service-ticket-form/addOrUpdateItme',
  serviceTicketFormAdd: sys + 'service-ticket-form/add',
  serviceTicketFormPublishSet: sys + 'service-ticket-form/publish/set',
  /**服务表单-可填写表单列表 */
  fillOutTheFormList: sys + 'service-ticket-form/fillin/list',
  serviceTicketFormTemplateDetail: sys + 'service-ticket-form/detail/get',
  serviceTicketFormOpenSet: sys + 'service-ticket-form/open/set',
  /**服务表单-表单模板列表 */
  formTemplateList: sys + 'service-ticket-form/manage/list',
  /**服务表单-删除模板 */
  deleteServiceTicketForm: sys + 'service-ticket-form/delete',
  /**表单数据-统计列表 */
  formStatisticsList: sys + 'service-ticket-form-user-answer/getByIdStatistics',
  /**表单数据-统计详情列表 */
  statisticsDetailList: sys + 'service-ticket-form-user-answer/getByFormIdContentList',
  serviceTicketFormFillTemplateDetail: root + 'service-ticket-form/getById',
  addServiceTicketFormDto: root + 'service-ticket-form/addServiceTicketFormDto',
  /**填写记录统一接口 */
  serviceTicketFormSnapshootDetail: root + 'service-ticket-form/snapshoot/details',
  serviceTicketFormRecordDetail: root + 'service-ticket-form/details',
  sysFillinRecordList: sys + 'service-ticket-form/fillin/record/list',
  appFillinRecordList: root + 'service-ticket-form/fillin/record/list',
  sysServiceTicketFormSnapshootDetail: sys + 'service-ticket-form/getRecordById',
  appServiceTicketFormSnapshootDetail: root + 'service-ticket-form/snapshoot/details',
  serviceTicketFormCopy: sys + 'service-ticket-form/copy',
   /** 导出记录 */
   exportFormList: sys + 'export/pageList',
   exportFormDetail: sys + 'export/get',
   asyncExportExcel: sys + 'service-ticket-form/asyncExportExcel',
};

/**
 * @description 获取后台管理员列表
 * @date 25/03/2024
 * @export
 * @param {IQuery} data
 */
export function sysUserList(data: IQuery) {
  return defHttp.get<IResData<SysUserResult>>({
    url: Api.sysUserList,
    data,
  });
}

/**
 * @description 创建服务表单模板 第一步
 * @date 26/03/2024
 * @export
 * @param {ServiceTicketFormAddData} data
 */
export function serviceTicketFormAdd(data: ServiceTicketFormAddData) {
  return defHttp.post<number>({
    url: Api.serviceTicketFormAdd,
    data,
  });
}

/**
 * @description 新建服务表单模板第二步, 表单项详情
 * @date 26/03/2024
 * @export
 * @param {number} id
 */
export function serviceTicketFormDetail(id: number) {
  return defHttp.get<ServiceTicketFormDetailResult>({
    url: Api.serviceTicketFormDetail + `/${id}`,
  });
}

/**
 * @description 新建服务表单模板第二步, 创建/编辑 表单项列表
 * @date 26/03/2024
 * @export
 * @param {ServiceTicketFormAddOrUpdateData} data
 */
export function serviceTicketFormAddOrUpdateItme(data: ServiceTicketFormAddOrUpdateItmeData) {
  return defHttp.post({
    url: Api.serviceTicketFormAddOrUpdateItme,
    data,
  });
}

/**
 * @description 新建服务表单模板第三步, 设置表单并发布
 * @date 26/03/2024
 * @export
 * @param {ServiceTicketFormPublishSetData} data
 */
export function serviceTicketFormPublishSet(data: ServiceTicketFormPublishSetData) {
  return defHttp.post({
    url: Api.serviceTicketFormPublishSet,
    data,
  });
}

/**
 * 服务表单- 可填写表单列表
 * @param data
 * @returns
 */
export function fillOutTheFormList(data: IQuery) {
  return defHttp.get<IResData<IFillOutTheForm>>({
    url: Api.fillOutTheFormList,
    data,
  });
}

/**
 * @description 服务表单模板详情
 * @date 26/03/2024
 * @export
 * @param {number} serviceTicketFormId
 */
export function serviceTicketFormTemplateDetail(serviceTicketFormId: number) {
  return defHttp.get<ServiceTicketFormTemplateDetailResult>({
    url: Api.serviceTicketFormTemplateDetail,
    data: {
      serviceTicketFormId,
    },
  });
}

/**
 * @description 服务表单模板开启/关闭
 * @date 26/03/2024
 * @export
 * @param {{ serviceTicketFormId: number, status: ServiceFormStatusEnum }} data
 */
export function serviceTicketFormOpenSet(data: {
  serviceTicketFormId: number;
  status: ServiceFormStatusEnum;
}) {
  return defHttp.post({
    url: Api.serviceTicketFormOpenSet,
    data,
  });
}

/*
 * 服务表单-表单模板列表
 * @param data
 * @returns
 */
export function formTemplateList(data: IQuery) {
  return defHttp.get<IResData<IFormTemplate>>({
    url: Api.formTemplateList,
    data,
  });
}

/**
 * 服务表单-删除模板
 * @param serviceTicketFormId
 * @returns
 */
export function deleteServiceTicketForm(serviceTicketFormId: number) {
  return defHttp.post({
    url: Api.deleteServiceTicketForm,
    data: { serviceTicketFormId }
  });
}


/**
 * 表单数据统计列表
 * @param data
 * @returns
 */
export function formStatisticsList(serviceTicketFormId: number) {
  return defHttp.get<FormStatistics[]>({
    url: Api.formStatisticsList,
    data: { serviceTicketFormId },
  });
}

/**
 * 表单数据统计详情列表
 * @param data
 * @returns
 */
export function statisticsDetailList(data: IQuery & { serviceTicketFormId: number, itemId: number }) {
  return defHttp.get<IResData<StatisticsDetailList>>({
    url: Api.statisticsDetailList,
    data,
  });
}

/**
 * @description 填写表单的 表单模板详情
 * @date 27/03/2024
 * @export
 * @param {number} serviceTicketFormId
 */
export function serviceTicketFormFillTemplateDetail(serviceTicketFormId: number) {
  return defHttp.get<ServiceTicketFormDetailResult>({
    url: Api.serviceTicketFormFillTemplateDetail + `/${serviceTicketFormId}`,
  })
}

/**
 * @description 提交服务表单或修改服务表单信息
 * @date 27/03/2024
 * @export
 * @param {AddServiceTicketFormDtoData} data
 */
export function addServiceTicketFormDto(data: AddServiceTicketFormDtoData) {
  return defHttp.post<{ id: number }>({
    url: Api.addServiceTicketFormDto,
    data
  })
}

/**
 * @description 填写记录 管理员
  *
 * @date 27/03/2024
 * @export
 * @param {IFormFillRecordListQuery} data
 */
export function sysFillinRecordList(data: IFormFillRecordListQuery) {
  return defHttp.get<IResData<IFormFillRecordList>>({
    url: Api.sysFillinRecordList,
    data,
  });
}

/**
 * 填写记录 普通会员
 * @param data
 * @returns
 */
export function appFillinRecordList(data: IFormFillRecordListQuery) {
  return defHttp.get<IResData<IFormFillRecordList>>({
    url: Api.appFillinRecordList,
    data,
  });
}

/**
 * @description 表单填写记录详情 (快照详情) 管理员
 * @date 27/03/2024
 * @export
 * @param {number} id
 */
export function sysServiceTicketFormSnapshootDetail(id: number) {
  return defHttp.get<ServiceTicketFormSnapshootDetailResult>({
    url: Api.sysServiceTicketFormSnapshootDetail + `/${id}`,
  })
}

/**
 * @description 表单填写记录详情 (快照详情) 普通会员
 * @date 27/03/2024
 * @export
 * @param {number} id
 */
export function appServiceTicketFormSnapshootDetail(id: number) {
  return defHttp.get<ServiceTicketFormSnapshootDetailResult>({
    url: Api.appServiceTicketFormSnapshootDetail + `/${id}`,
  })
}

/**
 * @description 表单填写记录详情 (实时同步表单项)
 * @date 27/03/2024
 * @export
 * @param {number} id
 */
export function serviceTicketFormRecordDetail(id: number) {
  return defHttp.get<ServiceTicketFormSnapshootDetailResult>({
    url: Api.serviceTicketFormRecordDetail + `/${id}`,
  })
}

/**
 * @description 复制服务表单模板
 * @date 28/03/2024
 * @export
 * @param {number} serviceTicketFormId
 */
export function serviceTicketFormCopy(serviceTicketFormId: number) {
  return defHttp.post<number>({
    url: Api.serviceTicketFormCopy,
    data: { serviceTicketFormId }
  })
}

/**
 * @description 导出表单记录-任务列表
 * @date 27/03/2024
 * @export
 */
export function getExportFormList(data: IQuery) {
  return defHttp.get<IResData<ServiceFormExportDetail>>({
    url: Api.exportFormList,
    data
  })
}

/**
 * @description 导出表单记录详情
 * @date 27/03/2024
 * @export
 * @param {string} taskId 多个逗号分割
 */
export function getExportFormDetail(taskId: string) {
  return defHttp.get<ServiceFormExportDetail[]>({
    url: Api.exportFormDetail + `/${taskId}`,
  })
}

/**
 * @description 导出表单-添加导出任务
 * @date 29/03/2024
 * @export
 * @param {string} id
 */
export function asyncExportExcel(id: number) {
  return defHttp.post({
    url: Api.asyncExportExcel + `/${id}`,
  })
}